package com.touchstone.cloudpacs.common.utils.result;

import com.github.pagehelper.PageInfo;
import com.touchstone.cloudpacs.common.utils.CommonUtils;
import lombok.Data;

/**
 * @Description : 返回结果封装
 * @Author : hunter
 * @Date: 2019-06-28 15:45
 * @Version: 1.0
 **/
@Data
public class ResultVO<T> {

    /**
     * 返回编码
     */
    private Integer code;

    /**
     * 返回信息提示
     */
    private String msg;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 返回记录数目
     */
    private Integer count;

    public ResultVO() {
    }

    public ResultVO(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResultVO(Integer code, String msg, Integer count, T data) {
        this.code = code;
        this.msg = msg;
        this.count = count;
        this.data = data;
    }

    public static ResultVO success() {
        return success(ResultEnum.SUCCESS.getDesc());
    }

    public static ResultVO success(String msg) {
        return success(msg, null, null);
    }

    public static <T> ResultVO success(T data) {
        return success(null, data, 0);
    }

    public static <T> ResultVO success(T data, Integer count) {
        return success(null, data, count);
    }

    public static <T> ResultVO success(String msg, T data, Integer count) {
        return new ResultVO(ResultEnum.SUCCESS.getStatusCode(), CommonUtils.isEmpty(msg) ? ResultEnum.SUCCESS.getDesc() : msg, count, data);
    }

    public static ResultVO NO_FOUND() {
        return new ResultVO(ResultEnum.NO_FOUND.getStatusCode(), ResultEnum.NO_FOUND.getDesc());
    }

    public static ResultVO error() {
        return new ResultVO(ResultEnum.ERROR.getStatusCode(), ResultEnum.ERROR.getDesc());
    }

    public static ResultVO error(String msg) {
        return new ResultVO(ResultEnum.ERROR.getStatusCode(), CommonUtils.isNotEmpty(msg) ? msg : ResultEnum.ERROR.getDesc());
    }

    public static ResultVO error(String msg, Integer errorCode) {
        return new ResultVO(errorCode, CommonUtils.isNotEmpty(msg) ? msg : ResultEnum.ERROR.getDesc());
    }

    public static ResultVO fail(String msg) {
        return new ResultVO(ResultEnum.FAIL.getStatusCode(), CommonUtils.isEmpty(msg) ? ResultEnum.FAIL.getDesc() : msg);
    }

    public static ResultVO success(PageInfo pageInfo) {
        if (pageInfo != null && pageInfo.getList() != null && pageInfo.getList().size() > 0) {
            return ResultVO.success(pageInfo.getList(), (int) pageInfo.getTotal());
        }
        return ResultVO.success();
    }

    public static <T extends PageInfo> ResultVO usePageInfo(T pageInfo) {
        return new ResultVO(ResultEnum.SUCCESS.getStatusCode(), ResultEnum.SUCCESS.getDesc(), (int) pageInfo.getTotal(), pageInfo.getList());
    }

    public static <T> ResultVO successResultCountVo(Integer count, T data) {
        return new ResultVO(ResultEnum.SUCCESS.getStatusCode(), ResultEnum.SUCCESS.getDesc(), count, data);
    }

    public static ResultVO noToken() {
        return new ResultVO(ResultEnum.NO_TOKEN.getStatusCode(), ResultEnum.NO_TOKEN.getDesc());
    }

    public static ResultVO tokenInvalid() {
        return new ResultVO(ResultEnum.TOKEN_INVALID.getStatusCode(), ResultEnum.TOKEN_INVALID.getDesc());
    }

    public static ResultVO tokenInvalid(String msg) {
        return new ResultVO(ResultEnum.TOKEN_INVALID.getStatusCode(), CommonUtils.isEmpty(msg) ? ResultEnum.TOKEN_INVALID.getDesc() : msg);
    }

    public static ResultVO noPermission() {
        return new ResultVO(ResultEnum.NO_PERMISSION.getStatusCode(), ResultEnum.NO_PERMISSION.getDesc());
    }

}
